package pta08;

import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

class Student{
	private int id;
	private String name;
	private int age;
	private double grade;
	
	public Student(){
		
	}
	public Student(int id, String name, int age, double grade) {
		this.id = id;
		this.setName(name);
		this.setAge(age);
		this.setGrade(grade);
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		if (name.length()>10){
			throw new IllegalArgumentException("name's length should <=10 "+name.length());
		}
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		if (age<=0){
			throw new IllegalArgumentException("age should >0 "+age);
		}
		this.age = age;
	}
	public double getGrade() {
		return grade;
	}
	@Override
	public String toString() {
		return "Student1 [id=" + id + ", name=" + name + ", age=" + age + ", grade=" + grade + "]";
	}
	public void setGrade(double grade) {
		if (grade<0 || grade >100){
			throw new IllegalArgumentException("grade should be in [0,100] "+grade);
		}
		this.grade = grade;
	}
	public void writeData(PrintWriter out) {
		 out.println(id + "|" + name + "|" + age+"|" +grade);
	}
	public void readData(Scanner in)
	   { 
		 String line = in.next();
	      String[] tokens = line.split("\\|");
	      id = Integer.parseInt(tokens[0]);
	      name=tokens[1];
	       age = Integer.parseInt(tokens[2]);
	    grade = Double.parseDouble(tokens[3]);
	   }
}

public class test1 {
	public static void main(String[] args) {
		Student[] students=new Student[3];
		students[0]=new Student(1,"张三",19,65);
		students[1]=new Student(2,"李四",19,75);
		students[2]=new Student(3,"王五",20,85);
		try
	      {//201521123013
	         PrintWriter out = new PrintWriter("student.txt");
	         out.println(students.length);
	         for (int i = 0; i < students.length; i++) {
	         out.println(students[i].getId()+"|"+students[i].getName()+"|"+students[i].getAge()+"|"+students[i].getGrade());
	         }
	      //   writeData(students, out);
	        // out.close();

	         Scanner in = new Scanner(new FileReader("student.txt"));
	         Student[] newStudent = readData(in);
	         in.close();
	         System.out.println(newStudent.length);
	         for (Student e : newStudent)
	            System.out.println(e);
	      }
	      catch (IOException exception)
	      {
	         exception.printStackTrace();
	      }
	}
	//201521123013
	private static void writeData(Student[] students, PrintWriter out){	    
	      out.println(students.length);
	      for (Student e : students)
	         e.writeData(out);
	}
	private static Student[] readData(Scanner in){
	      int n = in.nextInt();
	      in.nextLine();
	      Student[] students = new Student[3];
	      for (int i = 0; i < 3; i++)
	      {
	    	  students[i] = new Student();
	    	  students[i].readData(in);
	      }
	      return students;
	 }
}
